package com.yl.chainStore.payrule.coupons.entity

import com.yl.chainStore.common.Constants
import com.yl.chainStore.entity.pay.PayCustomerOrder
import com.yl.chainStore.payrule.coupons.CouponsRule
import com.yl.chainStore.payrule.common.RuleField
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import java.math.BigDecimal
import java.math.RoundingMode

/**
 * 满减订单对象（只能用一张）
 */
data class OrderFullReductionRule(

	/**
	 * 订单金额
	 */
	var payMoney:Long,

	/**
	 * 折扣金额
	 */
	var disCountMoney:Long,


): CouponsRule {

	/**
	 * 订单金额
	 */
	@RuleField(viewName = "订单金额")
	var payMoneyShow:BigDecimal?=null
		get() {
			if (field == null) {
				return BigDecimal(payMoney.div(Constants.OtherConfig.MONEY_TO_DB_DOUBLE)).setScale(2, RoundingMode.HALF_UP)
			}
			return field
		}
		set(value) {
			if (value != null) {
				payMoney = (value.multiply(BigDecimal(Constants.OtherConfig.MONEY_TO_DB_DOUBLE))).toLong()
				field = value
			}
		}

	/**
	 * 折扣金额
	 */
	@RuleField(viewName = "折扣金额")
	var disCountMoneyShow:BigDecimal?=null
		get() {
			if (field == null) {
				return BigDecimal(disCountMoney.div(Constants.OtherConfig.MONEY_TO_DB_DOUBLE)).setScale(2, RoundingMode.HALF_UP)
			}
			return field
		}
		set(value) {
			if (value != null) {
				disCountMoney = (value.multiply(BigDecimal(Constants.OtherConfig.MONEY_TO_DB_DOUBLE))).toLong()
				field = value
			}
		}

	override fun validationSelf(): ResponseBody {
		return returnSuccess()
	}


	override fun canUseForOrder(order: PayCustomerOrder): Boolean {
		return order.originalPrice >= payMoney
	}


}
